package net.hn.hnms.biz.emgcy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.hn.hnms.biz.emgcy.domain.RescueTeamPo;
import net.hn.hnms.biz.emgcy.domain.dto.ResuceTeamDto;
import net.hn.hnms.biz.emgcy.domain.vo.ResuceTeamVo;
import net.hn.hnms.biz.emgcy.mapper.RescueTeamMapper;
import net.hn.hnms.biz.emgcy.service.RescueTeamService;
import net.hn.hnms.biz.emgcy.util.FileUtil;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.system.api.RemoteDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: baijiajun
 * @Date: 2022/9/14
 */
@Service
public class RescueTeamServiceImpl extends ServiceImpl<RescueTeamMapper, RescueTeamPo> implements RescueTeamService {
    @Autowired
    private RescueTeamMapper rescueTeamMapper;
    @Autowired
    private RemoteDictService remoteDictService;

    @Override
    public ResponsePage<ResuceTeamVo> pageList(PageQuery pageQuery, ResuceTeamDto resuceTeamDto) {
        pageQuery.setPageNum(pageQuery.getPageNum() >= 1 ? (pageQuery.getPageNum() - 1) * pageQuery.getPageSize() : 0);
        List<ResuceTeamVo> list = rescueTeamMapper.pageList(pageQuery.getPageSize(), pageQuery.getPageNum(), resuceTeamDto);
        if (list.isEmpty()) {
            return new ResponsePage<>(new ArrayList(), 0);
        }
        return new ResponsePage<>(list, list.get(0).getTotal());
    }

    @Override
    public Boolean add(RescueTeamPo rescueTeamPo) {
        int insert = rescueTeamMapper.insert(rescueTeamPo);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean update(RescueTeamPo rescueTeamPo) {
        int update = rescueTeamMapper.updateById(rescueTeamPo);
        if (update > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean delete(Long id) {
        int delete = rescueTeamMapper.deleteById(id);
        if (delete > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void export(HttpServletResponse httpServletResponse) throws IOException {
        List<RescueTeamPo> list = rescueTeamMapper.selectList(null);
        List<Map<String, Object>> listExport = new ArrayList<>();
        list.forEach(item -> {
            Map<String, Object> map = new LinkedHashMap<>();
            if (StringUtils.isNotEmpty(item.getTeamTypeCode())) {
                String label = remoteDictService.selectDictLabel("team_type", item.getTeamTypeCode());
                if (null != label) {
                    map.put("队伍类型", label);
                } else {
                    map.put("队伍类型", null);
                }
            } else {
                map.put("队伍类型", null);
            }
            map.put("队伍名称", item.getName());

            if (StringUtils.isNotEmpty(item.getQualificationCode())) {
                String label1 = remoteDictService.selectDictLabel("team_attributes_type", item.getQualificationCode());
                if (null != label1) {
                    map.put("资质等级", label1);
                } else {
                    map.put("资质等级", null);
                }
            } else {
                map.put("资质等级", null);
            }
            map.put("责任单位", item.getResponsibleUnit());
            map.put("第一负责人", item.getContact());
            map.put("联系电话", item.getContactTel());
            map.put("队伍人数", item.getTotalPerNum());
            map.put("小组职责", item.getDuty());
            /*map.put("成立时间", item.getFoundedDate());
            map.put("主要装备情况", item.getEquipDesc());
            map.put("专长描述", item.getSpecialityDesc());
            map.put("应急救援经历", item.getEmgcyResume());*/
            listExport.add(map);
        });
        FileUtil.downloadExcel(listExport, httpServletResponse);
    }
}
